package com.ruoyi.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitmqConfig {
    //声明一个业务交换机
    @Bean
    public FanoutExchange bizExchange(){
        return new FanoutExchange("bizExchange");
    }

    //声明第第一个队列   共二个
    @Bean
    public Queue bizQueue1() {
        HashMap<String, Object> map = new HashMap<>();
        // x-dead-letter-exchange   声明当前队列绑定的死信交换机
        map.put("x-dead-letter-exchange","deadLetterExchange");
        //x-dead-letter-routing-key   这里声明当前队列的死信路由key
        map.put("x-dead-letter-routing-key","deadKey1");
        map.put("x-message-ttl", 20000);
        return QueueBuilder.durable("bizQueue1").withArguments(map).build();
    }

    //业务队列2
    @Bean
    public Queue bizQueue2() {
        //队列1和死信交换机绑定
        HashMap<String, Object> map = new HashMap<>();
        //x-dead-letter-exchange   这里是声明当前队列绑定的死信交换机
        map.put("x-dead-letter-exchange","deadLetterExchange");
        //x-dead-letter-routing-key 这里是声明当前队列的死信交换机
        map.put("x-dead-letter-routing-key","deadKey2");
        map.put("x-message-ttl",20000);
        return QueueBuilder.durable("bizQueue2").withArguments(map).build();
    }

    //让交换机和队列绑定
    @Bean
    public Binding binding1(@Qualifier("bizQueue1") Queue queue,@Qualifier("bizExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean
    public Binding binding2(@Qualifier("bizQueue2") Queue queue,@Qualifier("bizExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    /////////////////////////////////死信配置

    //声明死信Exchange
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("deadLetterExchange");
    }

    //声明死信队列A
    @Bean("deadLetterQueue1")
    public Queue deadLetterQueueA(){
        return new Queue("deadLetterQueue1");
    }

    //声明死信队列B
    @Bean("deadLetterQueue2")
    public Queue deadLetterQueueB() {
        return new Queue("deadLetterQueue2");
    }

    //绑定死信交换机和死信队列的关系
    //声明私信队列A绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueue1") Queue queue, @Qualifier("deadLetterExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with("deadKey1");
    }

    //声明死信队列B绑定关系
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueue2") Queue queue, @Qualifier("deadLetterExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with("deadKey2");
    }

}